package io.jchat.android.application;

import android.content.ContentResolver;
import android.content.Intent;
import android.app.Activity;
import android.graphics.Color;
import android.widget.Toast;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.PluginResult;
import android.util.Log;
import android.provider.Settings;
import android.os.Bundle;
import android.widget.Toast;
import org.json.JSONArray;
import java.util.Collections;
import java.util.List;
import java.io.File;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.model.Conversation;
import io.jchat.android.tools.SortConvList;
import org.json.JSONException;
import org.json.JSONObject;
import io.jchat.android.controller.LoginController;
import io.jchat.android.activity.MainActivity;
import io.jchat.android.activity.FixProfileActivity;
import io.jchat.android.activity.ReloginActivity;
import io.jchat.android.application.JChat;
import io.jchat.android.chatting.ChatActivity;

public class WHFJMessageHelper extends CordovaPlugin {

	public static final String TAG = "GDJMessage Plugin";
	private CallbackContext callbackContext;
	private static String action_login = "login";
	private static String action_regist = "regist";
	private static String action_main = "getAllConversationList";
	private static String action_singleConv = "creatConversation";
	private static String action_logout = "logout";
	private static String action_update_pwd = "updatePwd";
	private static String action_go_message = "test";
	private static String action_upd_info = "resetInfo";
	private static String action_msg="msg";
	private FixProfileActivity fpfa;
	private JChat jchat;
	private LoginController lc;
	final int duration = Toast.LENGTH_SHORT;
    private String msg="";
	/**
	 * Constructor.
	 */
	public WHFJMessageHelper() {
	}
	public WHFJMessageHelper(String msg) {
	
	this.msg=msg;
	}
	/**
	 * Sets the context of the Command. This can then be used to do things like
	 * get file paths associated with the Activity.
	 *
	 * @param cordova
	 *            The context of the main Activity.
	 * @param webView
	 *            The CordovaWebView Cordova is running in.
	 */

	public void initialize(CordovaInterface cordova, CordovaWebView webView) {
		super.initialize(cordova, webView);
		Log.v(TAG, "Init CoolPlugin");
	}

	public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

		// Shows a toast
		Log.v(TAG, "chenyue:GDJMessage received:" + action);

		if (action_login.equals(action)) {
			System.out.println("chenyue:GDJMessage-->action_login-->args.getString(0)��" + args.getString(0)
					+ ";args.getString(1):" + args.getString(1));
		   if( login(args.getString(0), args.getString(1))==true){
		    	callbackContext.success("login is true");
			      return true;
		         }else{
		          	callbackContext.error("login:" + false);
			          return false;
	         	}

		}	
		else if (action_go_message.equals(action)) {
			jchat = new JChat();
System.out.println("chenyue:GDJMessage-->action_go_message.equals>args.getString(0)��" + args.getString(0));
			if (jchat.goMessage(args.getString(0)) == true) {
				callbackContext.success("goMessage is true");
				return true;
			} else {
				callbackContext.error("goMessage:" + false);
				return false;
			}
		} 
		
		else if (action_regist.equals(action)) {
			return regist(args.getString(0), args.getString(1));
		} 
		
		else if (action_main.equals(action)) {
			System.out.println("chenyue:GDJMessage-->action_main-->args.getString(0)��" + args.getString(0));
            if(args.getString(0).equals("null")){
            	
            	callbackContext.success(getUnreadMsg());
            }
            if(args.getString(0).equals("0")){
			   if (play(MainActivity.class, args.getString(0), action) == true) {
				     jchat = new JChat();				  
				     callbackContext.success(jchat.s);			
			    } else {
				     callbackContext.error("Main:" + true);
				     return false;
			    }
			  }
            return true;
		}
		else if (action_logout.equals(action)) {
			
			System.out.println("chenyue:GDJMessage-->action_logout-->args.getString(0)��");
		
			jchat = new JChat();
			// Log.v(TAG,"chenyue:GDJMessage nothing to open!");
			return jchat.logout();
			
		} 
		
		else if (action_update_pwd.equals(action)) {
			
			System.out.println("chenyue:GDJMessage-->action_update_pwd-->args.getString(0)��");
			jchat = new JChat();
			// Log.v(TAG,"chenyue:GDJMessage nothing to open!");
			if (jchat.updatePwd(args.getString(0), args.getString(1)) == true) {
				
				callbackContext.success("updatePwd is true");
				return true;
			} else {
				callbackContext.error("updatePwd:" + false);
				return false;
			}
		} 
		
		else if (action_singleConv.equals(action)) {
		//jchat = new JChat();
	//	Log.v(TAG, "chenyue:GDJMessage-->action_singleConv--> conv to open:" + jchat.IDConv(args.getString(0)));
			if (play(MainActivity.class,"0",args.getString(0)) == true) {
			  	
			     callbackContext.success("IDConv is true");	
			     return true;			
			} else {
				callbackContext.error("IDConv:" + false);
				return false;
			}
		} 
		else if(action_msg.equals(action)){	  	        
	  	  	Log.v(TAG, "chenyue:GDJMessage action to open!action:"+msg);
	  	        if(action_msg.equals(action)){
	  	        	jchat = new JChat();					 
					callbackContext.success(jchat.s);
	  	        	return true;
	  	        }else{
	  	        	jchat = new JChat();
	  	        	 callbackContext.error(jchat.s);
	  	        	return false;
	  	        }
		     
			   
			
			
		}
		else if(action_upd_info.equals(action)){
			 System.out.println("chenyue:GDJMessage-->action_upd_info-->args.getString(0):"+args.getString(0));
    	     System.out.println("chenyue-->myselect-->开始处理用户请求");
			     if(args.getString(1).equals("0")){			        
            	          fpfa=new FixProfileActivity();
            	          fpfa.uploadUserAvatar(args.getString(0));      	                       	   
		         }else if(args.getString(1).equals("1")){
		    	          jchat = new JChat();	
		    	          jchat.updateNK(args.getString(0));
		          }
			
			 return true;
        }
		 else{
			 return false;
		}

	};

	public boolean login(String name, String pwd) {
		lc = new LoginController();
		boolean state = lc.goMain(name, pwd, null);
		if (state == true) {
			callbackContext.success();
		} else {
			this.callbackContext.error("login is failed");
		}
		return state;

	};

	public boolean regist(String name, String pwd) {
		lc = new LoginController();
		boolean state = lc.goRegist(name, pwd);
		if (state == true) {
			callbackContext.success("goRegist is true");
		} else {
			this.callbackContext.error("regist is failed");
		}
		return state;

	};
    public int getUnreadMsg(){
        List<Conversation> mDatas=JMessageClient.getConversationList();
        SortConvList  scl=new SortConvList();
        Collections.sort(mDatas, scl);
      
        int urMsg=0;
            for(Conversation conv:mDatas){
            	urMsg+=	conv.getUnReadMsgCnt();
            	
            }
        	System.out.println("chenyue:GDJMessage-->action_main-->getUnreadMsg:"+urMsg);
    		
           return urMsg;
    }
	public boolean play(final Class activityClass, final String argsA, final String argsB) {
		final CordovaInterface cordovaObj = cordova;
		final CordovaPlugin plugin = this;
		cordova.getActivity().runOnUiThread(new Runnable() {
			public void run() {
				final Intent streamIntent = new Intent(cordovaObj.getActivity().getApplicationContext(), 

activityClass);
				Bundle extras = new Bundle();

				extras.putString("param0", argsA);
				extras.putString("param1", argsB);
				Toast toast = Toast.makeText(cordova.getActivity().getApplicationContext(), argsB, duration);
				toast.show();
				streamIntent.putExtras(extras);
				cordovaObj.startActivityForResult(plugin, streamIntent, 200);

			}
		});

		return true;
	}

}
